package cn.edu.jnu.supershopper.Game;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;

public class FlyingObject {
    //飞机的坐标
    public int posX,posY;
    //飞机宽高
    public int width,height;
    //运动的速度
    public int speed;
    //资源文件
    public Bitmap bitmap;

    public FlyingObject(Context context,int resourceId){
        bitmap = BitmapFactory.decodeResource(context.getResources(),resourceId);
        width = bitmap.getWidth();
        height = bitmap.getHeight();
    }

    public void draw(Canvas cv, Paint pt){
        cv.drawBitmap(bitmap,posX,posY,pt);
    }

    public void move(){
        posY+=speed;
    }

    //碰撞检查
    public Boolean touchCheck(FlyingObject obj){
        // 判断两个矩形是否相交
        // 分别比较两个矩形重心在X轴方向、Y轴方向的距离，与两个矩形的长、宽的一半的和的大小，小于则相交
        int dist_x = Math.abs((this.posX+this.width/2)-(obj.posX+obj.width/2));
        int x = (this.width+obj.width)/2;
        int dist_y = Math.abs((this.posY+this.height/2)-(obj.posY+obj.height/2));
        int y = (this.height+obj.height)/2;
        return dist_x <= x && dist_y <= y;
    }

    public String toString() {
        return "Object [posX=" + posX + ", posY=" + posY + ", width=" + width
                + ", height=" + height + ", speed=" + speed + ", bitmap="
                + bitmap + "]";
    }
}

